package com.example.soa_exp2.Controller;

import com.example.soa_exp2.Config.CommonResponse;
import com.example.soa_exp2.Domain.Goods;
import com.example.soa_exp2.Domain.Student;
import com.example.soa_exp2.Service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
@Tag(name = "器材信息管理模块")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @PostMapping
    @Operation(summary = "新增器材")
    public CommonResponse<Goods> addGoods(@RequestBody Goods good){
        if(good.getGoods_id()==null){
            return CommonResponse.BackForError("器材编号为空，新增器材失败");
        } else if (good.getGoods_name()==null) {
            return CommonResponse.BackForError("器材名称为空，新增器材失败");
        } else if (good.getStock() == null) {
            return CommonResponse.BackForError("器材数量为空，新增器材失败");
        } else {
            int result = goodsService.addGoods(good);
            if (result == 0){
                return CommonResponse.BackForError("新增器材失败");
            }else {
                return CommonResponse.BackForSuccess("新增器材成功","http://127.0.0.1:8081/goods");
            }
        }
    }

    @Operation(summary = "修改器材信息")
    @PutMapping
    public CommonResponse<Goods> updateGoods(@RequestBody Goods good){
        if(good.getGoods_id()==null){
            return CommonResponse.BackForError("器材编号为空，修改器材信息失败");
        } else if (good.getGoods_name()==null) {
            return CommonResponse.BackForError("名称为空，修改器材信息失败");
        } else if (good.getStock()==null) {
            return CommonResponse.BackForError("器材数量为空，修改器材信息失败");
        }else{
            int result = goodsService.updateGoods(good);
            if(result == 0){
                return CommonResponse.BackForError("修改器材信息失败");
            }else {
                return CommonResponse.BackForSuccess("修改器材信息成功","http://127.0.0.1:8081/goods");
            }
        }
    }

    @Operation(summary = "删除器材信息")
    @DeleteMapping
    public CommonResponse<Goods> deleteGoodsById(String goods_id){
        int result = goodsService.deleteGoods(goods_id);
        if(result == 0){
            return CommonResponse.BackForError("删除器材信息失败");
        }else{
            return CommonResponse.BackForSuccess("删除器材信息成功","http://127.0.0.1:8081/goods");
        }
    }

    @Operation(summary = "通过编号搜索器材信息")
    @GetMapping("/byId")
    public CommonResponse<Goods> getGoodsById(@RequestParam("goods_id") String goods_id){
        Goods good = goodsService.getGoodsById(goods_id);
        if (good == null) {
            return CommonResponse.BackForError("未搜索到相关器材信息");
        }else {
            return CommonResponse.BackForSuccess("通过编号搜索器材信息成功", good,"http://127.0.0.1:8081/goods");
        }
    }

    @Operation(summary = "通过名称搜索器材信息")
    @GetMapping("/byName")
    public CommonResponse<List<Goods>> getGoodsByName(@RequestParam("name") String name){
        List<Goods> goods = goodsService.getGoodsByName(name);
        if(goods == null){
            return CommonResponse.BackForError("未搜索到相关器材信息");
        }else {
            return CommonResponse.BackForSuccess("通过名称搜索器材信息成功", goods,"http://127.0.0.1:8081/goods");
        }
    }
}
